<template>
    <div>
        新增角色
        <el-form ref="form" :model="form" label-width="80px">

            <el-form-item label="角色名">
                <el-input v-model="form.roleName"></el-input>
            </el-form-item>

            <el-form-item label="描述信息">
                <el-input v-model="form.description"></el-input>
            </el-form-item>


            <el-form-item label="角色权限">
                <el-select v-model="selectPermissionIds" multiple placeholder="请选择">
                    <el-option v-for="item in query_permissions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                <router-link to="/home/role">
                    <el-button> 取消 </el-button>
                </router-link>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>

import { getPermissionList } from '@/api/permission.js'
import { addRole } from '@/api/role.js'

export default {
    data() {
        return {
            form:{
                roleName: "",
                description: "",
                permissions: []
            },
            query_permissions:[],
            selectPermissionIds:[]
        }
    },
    methods: {
        onSubmit() {
            for (let i = 0; i < this.selectPermissionIds.length; i++) {
                let obj = { permissionId: this.selectPermissionIds[i] }
                this.form.permissions.push(obj)
            }
            
            addRole(this.form).then((result) => {

                    this.$message.success('新增成功')
                    this.$router.push('/home/role')

            }).catch((err) => {

            });
        }
    },
    mounted(){
        getPermissionList().then((result) => {
            this.query_permissions = result.data.map(function (obj) {
                return { value: obj.permissionId, label: obj.permissionName };
            });
        }).catch((err) => {
            
        });
    }
}
</script>

<style>